# -*- coding: utf-8 -*-
import __main__
from appengine_utilities import sessions
from appengine_utilities import flash
from appengine_utilities import event
from appengine_utilities import cache
from appengine_utilities.rotmodel import ROTModel

import wsgiref.handlers
import xmw
from xmw.db import ds
from google.appengine.ext import webapp
from google.appengine.ext import db
import urllib
from xmw import error
import logging
from xmw.db import user_mgr
import xmlrpclib
import global_setting


class MainPage(xmw.RequestHandler):
    def get_req(self,status):
        q=ds.Sub_Req().all()
        q.filter('status =',status)
        #q.filter('status =',ds.Sub_Req_Status.INIT)
        req=q.get()
        return req
    def get_INIT_req(self):
        return self.get_req(ds.Sub_Req_Status.INIT)
    def get_ACCEPTED_req(self):
        return self.get_req(ds.Sub_Req_Status.ACCEPTED)
    
    def get(self):
        #handle "INIT"
        e =None
        #site=global_setting.get_bot_site()
        site="http://xmw-bot-4.appspot.com/"
        url=site+'xmlrpc/thread_mgr'
        s = xmlrpclib.Server(url)
        
        req=self.get_INIT_req()
        if req is not None:
            (ret,e_dict)=s.thead_mgr.reg_url(req.url)
            e=error.Error.build_error(e_dict)
            if e.code == error.URL_ACCEPTED.code:
                #accepted
                req.status=ds.Sub_Req_Status.ACCEPTED
                #req.put()
                req.save()
            else:
                logging.info('e.code:%s'%str(e.code))
                #url=req.url      
                logging.info('gonna fill the req directly,req_id:%s'%req.sub_req_id) 
                q_thread=xmw.Thread.all()
                
                q_thread.filter('lst_page_url =', str(req.url))
                thread = q_thread.get()
                if thread is not None:
                    logging.info('found thread with thread_id:%s'%thread.thread_id)
                    req.thread_id=thread.thread_id
                    req.status = ds.Sub_Req_Status.IN_TRACE
                    req.save()
                else:
                    logging.info('not found thread by url:%s'%req.url)
                    #if there concurrent subs,it will comes to here and alert some errors,
                    #and then come down!
                    logging.error(e_dict)
            #self.render_error(e)
        
        req=self.get_ACCEPTED_req()
        
        if req is not None:
            thread_id=s.thead_mgr.query_thread_id_by_url(req.url)
            if thread_id is not None:
                req.thread_id=thread_id
                req.status=ds.Sub_Req_Status.IN_TRACE
                #req.put()
                req.save()
            e=error.OK("got thread_id:%s"%thread_id)
        if e is None:
            e=error.OK("None")
        e=error.OK(str(e))
        self.render_error(e)
        
def main():
  application = webapp.WSGIApplication(
                                       [('/robot/sub_daemon', MainPage)],
                                       debug=True)
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
  main()